package com.wskj.workbench.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wskj.common.core.mybatisplus.core.ServicePlusImpl;
import com.wskj.common.core.page.PagePlus;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.utils.common.AssertUtils;
import com.wskj.common.utils.common.PageUtils;
import com.wskj.workbench.bo.web.TbRedisKeyAddBo;
import com.wskj.workbench.bo.web.TbRedisKeyEditBo;
import com.wskj.workbench.bo.web.TbRedisKeyQueryBo;
import com.wskj.workbench.domain.TbRedisKey;
import com.wskj.workbench.mapper.TbRedisKeyMapper;
import com.wskj.workbench.service.ITbRedisKeyService;
import com.wskj.workbench.vo.web.TbRedisKeyVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * redis的过期键信息Service业务层处理
 *
 * @author wsdz
 * @date 2022-09-29
 */
@Service
public class TbRedisKeyServiceImpl extends ServicePlusImpl<TbRedisKeyMapper, TbRedisKey> implements ITbRedisKeyService {

    @Override
    public TbRedisKeyVo queryById(String redisKey) {
        return getVoById(redisKey, TbRedisKeyVo.class);
    }

    @Override
    public TableDataInfo<TbRedisKeyVo> queryPageList(TbRedisKeyQueryBo bo) {
        PagePlus<TbRedisKey, TbRedisKeyVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), TbRedisKeyVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<TbRedisKeyVo> queryList(TbRedisKeyQueryBo bo) {
        return listVo(buildQueryWrapper(bo), TbRedisKeyVo.class);
    }

    private LambdaQueryWrapper<TbRedisKey> buildQueryWrapper(TbRedisKeyQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbRedisKey> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getRedisKey()), TbRedisKey::getRedisKey, bo.getRedisKey());
        lqw.eq(StrUtil.isNotBlank(bo.getEndTime()), TbRedisKey::getEndTime, bo.getEndTime());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(TbRedisKeyAddBo bo) {
        TbRedisKey add = BeanUtil.toBean(bo, TbRedisKey.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(TbRedisKeyEditBo bo) {
        TbRedisKey update = BeanUtil.toBean(bo, TbRedisKey.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TbRedisKey entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importTzRedisKey(List<TbRedisKeyVo> tzRedisKeyList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(tzRedisKeyList), "导入数据不能为空");
        int successNum = 0;
        for (TbRedisKeyVo tzRedisKey : tzRedisKeyList) {
            TbRedisKey add = BeanUtil.toBean(tzRedisKey, TbRedisKey.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", tzRedisKeyList.size(), successNum);
        if (successNum != tzRedisKeyList.size()) {
            msg = msg + String.format("失败 %d 条！", tzRedisKeyList.size() - successNum);
        }
        return msg;
    }

    @Override
    public int addRedisKey(String redisKey, String endTime) {
        TbRedisKey tzRedisKey = new TbRedisKey();
        tzRedisKey.setRedisKey(redisKey);
        tzRedisKey.setEndTime(endTime);
        return baseMapper.insert(tzRedisKey);
    }

    @Override
    public int deleteRedisKey(String redisKey) {
        LambdaQueryWrapper<TbRedisKey> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TbRedisKey::getRedisKey, redisKey);
        return baseMapper.delete(lambdaQueryWrapper);
    }
}
